package com.weight.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.weight.controller.utils.Result;
import com.weight.entity.Weight;
import com.weight.service.WeightService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/weight")
public class WeightController {
    @Autowired
    private WeightService weightService;
    @PostMapping("add")
    public Result add(@RequestBody Weight weight){
        String reg = "^-?[0-9]+(.[0-9]+)?$";
        if (!StringUtils.hasLength(weight.getWeight())) return Result.error("输入不能为空");
        if (!weight.getWeight().matches(reg)) return Result.error("请输入数字");
        if (!StringUtils.hasLength(weight.getUserId())) return Result.error("id不能为空");
        Date date = new Date();
        QueryWrapper<Weight> weightQueryWrapper = new QueryWrapper<>();
        String strDateFormat = "yyyy-MM-dd";
        SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);
//        weightQueryWrapper.between("create_time",sdf.format(date)+" "+"00:00:00",sdf.format(date)+" "+"23:59:59");
        weightQueryWrapper.eq("user_id",weight.getUserId()).like("create_time",sdf.format(date));
        //如果今天有，就编辑，没有就新增
        if (weightService.count(weightQueryWrapper)>0){
            UpdateWrapper<Weight> up = new UpdateWrapper<>();
            up.set("weight",weight.getWeight()).set("update_time",date).eq("user_id",weight.getUserId()).between("create_time",sdf.format(date)+" "+"00:00:00",sdf.format(date)+" "+"23:59:59");
            weightService.update(null,up);
            return  Result.success(null,"更新成功");
        }else {
            weightQueryWrapper = null;
            weight.setId(String.valueOf(UUID.randomUUID()).replaceAll("-",""));
            weight.setCreateTime(date);
            weight.setUserId(weight.getUserId());
            weightService.save(weight);
            return  Result.success(null,"记录成功");
        }
    }


    //获取体重列表
    @GetMapping("weightList")
    public Result weightList(String id){
        QueryWrapper<Weight> qw = new QueryWrapper<>();
        qw.select("weight","create_time").eq("user_id",id).last("limit 5");
        List<Weight> list = weightService.list(qw);
        System.out.println(list);
        qw = null;
        JSONArray jsonArray = new JSONArray();
        for (Weight weight1 : list) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("label",new SimpleDateFormat("MM-dd").format(weight1.getCreateTime()));
            jsonObject.put("value",weight1.getWeight());
            jsonArray.add(jsonObject);
        }
        list = null;
        return Result.success(jsonArray,"查寻成功");
    }
}
